package modules

import (
	"missevanbot/models"
	"missevanbot/utils"
)

const (
	sufEvent   = "event"
	sufCommand = "command"
	sufGame    = "game"
	sufTask    = "task"
	sufLucky   = "lucky"
	sufInfo    = "info"
)

func SetStatsEvent(rid int64, event string, incr int64) {
	setStats(rid, sufEvent, event, incr) // missevan:[RoomID]:stats:event
}

func SetStatsCommand(rid int64, cmd string) {
	setStats(rid, sufCommand, cmd, 1) // missevan:[RoomID]:stats:command
}

func SetStatsGame(rid int64, game string) {
	setStats(rid, sufGame, game, 1) // missevan:[RoomID]:stats:game
}

func SetStatsTask(rid int64, task string) {
	setStats(rid, sufTask, task, 1) // missevan:[RoomID]:stats:task
}

func SetStatsLucky(rid int64, lucky, gift models.FmGift) {
	setStats(rid, sufLucky, "lucky", lucky.Number*lucky.Price)
	setStats(rid, sufLucky, "gift", gift.Number*gift.Price)
}

func SetStatsInfo(rid int64, info string) {
	setStats(rid, sufInfo, info, 1) // missevan:[RoomID]:stats:info
}

func setStats(rid int64, suffix, field string, incr int64) {
	key := prefixRoom(rid) + "stats:" + suffix // missevan:[RoomID]:stats:[suffix]
	rdb.HIncrBy(ctx, key, field, incr)
}

func StatsEvent(rid int64) map[string]int64 {
	return stats(rid, sufEvent)
}

func StatsCommand(rid int64) map[string]int64 {
	return stats(rid, sufCommand)
}
func StatsGame(rid int64) map[string]int64 {
	return stats(rid, sufGame)
}
func StatsTask(rid int64) map[string]int64 {
	return stats(rid, sufTask)
}

func StatsLucky(rid int64) map[string]int64 {
	return stats(rid, sufLucky)
}

func StatsInfo(rid int64) map[string]int64 {
	return stats(rid, sufInfo)
}

func stats(rid int64, suffix string) map[string]int64 {
	key := prefixRoom(rid) + "stats:" + suffix // missevan:[RoomID]:stats:[suffix]
	val := rdb.HGetAll(ctx, key).Val()
	m := make(map[string]int64)
	for k, v := range val {
		n, _ := utils.Atoi(v)
		m[k] = n
	}
	return m
}
